﻿package me.nerik.air 
{
	import flash.desktop.NativeApplication;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.InvokeEvent;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	
	/**
	* To be extended (or not)
	* @author erik
	*/
	public class AppSettings extends EventDispatcher
	{
	
		private static var _instance:AppSettings = new AppSettings();
		
		public function AppSettings() 
		{
			if (_instance) throw new Error( "Singleton and can only be accessed through Singleton.getInstance()" );
		}

		public static function get instance():AppSettings 
		{
			return _instance;
		}
		
		
		public static const READY:String = "ready";
		private static const DEFAULT_XML_PATH:String = ".settings.xml";
		
		private var _invoked:Boolean;
		private var _loaded:Boolean;
		
		private var _commandLineArgs:Array;
		private var _xmlPath:String;
		private var _xmlFile:File;
		public  var data:XML;
		//private var _appXml:XML;
		//private var _appId:String;
		//private var _appVersion:String;
		
		
		

		public function initialize():void  
		{
			_xmlPath = DEFAULT_XML_PATH;
			NativeApplication.nativeApplication.addEventListener(InvokeEvent.INVOKE, onInvoked);
		}
		
		private function onInvoked(e:InvokeEvent):void 
		{
			
			if (_invoked) return;
			
			NativeApplication.nativeApplication.removeEventListener(InvokeEvent.INVOKE, onInvoked);
			
			
			var args:Array = e.arguments;
			var numArgs:int = args.length;
			
			for (var i:int = 0; i < numArgs; i++) 
			{
				_commandLineArgs.push(args[i]);
			}
					
			_invoked = true;
			
			if (!_loaded) load();
		}
		
		private function load():void
		{
			if (!_invoked) return;
						
			/*//get app info from the XML descriptor
			_appXml = NativeApplication.nativeApplication.applicationDescriptor;
			var installedNs:Namespace = _appXml.namespace();
			
			_appVersion = _appXml.installedNs::version;
			_appId = _appXml.installedNs::id;
						*/
			
			//settings xml
			_xmlFile = File.applicationStorageDirectory.resolvePath(_xmlPath);
			
					
			//if the xml file already exists, we load it
			if (_xmlFile.exists)
			{		
				data = loadXml();
				
				if (data.firstRun == undefined)
				{
					data.appendChild(<firstRun>false</firstRun>);
					saveXml();
				}
				
			}
			else
			{
				trace("create xml...");
				data = <settings></settings>;

				saveXml();				
				
			}
			
			XML.prettyPrinting = false;
						
		
			imReady();
	
			_loaded = true;
			
			
		}
		
		
		private function loadXml():XML
		{
			
			var fs:FileStream = new FileStream();	
			fs.open(_xmlFile, FileMode.READ);
			
			var tmpXml:XML = XML(fs.readUTFBytes(fs.bytesAvailable));
			
			fs.close();
			
			return tmpXml;			
			
		}
		
		public function saveXml():void
		{
			//trace(_xml.toXMLString())
			var fs:FileStream = new FileStream();	
			fs.open(_xmlFile, FileMode.WRITE);
					
			fs.writeUTFBytes(data.toXMLString());
			fs.close();
			
		} 
		
		
		protected function imReady():void
		{
			dispatchEvent(new Event(READY));
		}

		
		
		public function get commandLineArgs():Array { return _commandLineArgs; }
				
		public function get xmlNativePath():String { return _xmlFile.nativePath; }
		
		
		//actual settings getters
		
		public function get firstRun():Boolean
		{
			
			if(data.firstRun == undefined) return true;
			return data.firstRun == "true";
		}

	}

}

 